package com.nf.health.app.models;

import android.preference.PreferenceManager;

import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;
import com.nf.health.app.utils.PreferenceHelper;

/**
 * Created by fei on 2016/4/13.
 */
public class UserProfileManager {

    private static UserProfileManager userProManager;

    public static UserProfileManager getUserProfileManager() {
        if (userProManager == null) {
            userProManager = new UserProfileManager();
        }
        return userProManager;
    }
    private UserProfileManager() {
    }
    private EaseUser currentUser;
    public synchronized EaseUser getCurrentUserInfo() {
        if (currentUser == null) {
            currentUser = new EaseUser(PreferenceHelper.getString(PreferenceHelper.USERID, ""));
            String nick = getCurrentUserNick();
            currentUser.setNick((nick != null) ? nick : "");
            currentUser.setAvatar(getCurrentUserAvatar());
        }
        return currentUser;
    }

    public synchronized void reset() {
        currentUser = null;
        PreferenceHelper.remove(PreferenceHelper.KEY_NAME);
    }

    private void setCurrentUserNick(String nickname) {
        getCurrentUserInfo().setNick(nickname);
        PreferenceHelper.putString(PreferenceHelper.USERNICK, nickname);
    }

    private void setCurrentUserAvatar(String avatar) {
        getCurrentUserInfo().setAvatar(avatar);
        PreferenceHelper.putString(PreferenceHelper.USERICON, avatar);
    }

    private String getCurrentUserNick() {
        return PreferenceHelper.getString(PreferenceHelper.USERNICK, "");
    }

    private String getCurrentUserAvatar() {
        return PreferenceHelper.getString(PreferenceHelper.USERICON, "");
    }
}
